package com.thinksns.sociax.t4.android.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.thinksns.sociax.adapter.AdapterWeiboDigg;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.adapter.AdapterSociaxList;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelDiggUser;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;

public class FragmentWeiboDigg extends FragmentSociax implements OnRefreshListener,OnLoadmoreListener{

    private SmartRefreshLayout smartRefreshLayout;
    private int weibo_id;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_userlist;
    }

    @Override
    public void initView() {
        smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_refresh_layout);
        listView = (ListView) findViewById(R.id.pull_refresh_list);
        listView.setDivider(new ColorDrawable(getResources().getColor(R.color.bg_ios)));
        listView.setDividerHeight(1);
        listView.setSelector(getResources().getDrawable(R.drawable.listitem_selector));

        adapter = createAdapter();
        listView.setAdapter(adapter);
    }

    @Override
    public AdapterSociaxList createAdapter() {
        list = new ListData<SociaxItem>();
        return new AdapterWeiboDigg(FragmentWeiboDigg.this, list, weibo_id);
    }

    @Override
    public void initIntentData() {
        weibo_id = getActivity().getIntent().getIntExtra("weibo_id", -1);
    }

    @Override
    public void initListener() {
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadmoreListener(this);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ModelDiggUser user = (ModelDiggUser) adapter.getItem((int) id);
                if (Thinksns.getMy().getUid() != user.getUid() && ModelUser.CAN_NOT_TO_HOME == user.getSpace_privacy()){
                    ToastUtils.showToast(R.string.tip_no_oauth_to_other_home);
                }else {
                    Intent intent = new Intent(getActivity(), ActivityUserInfo_2.class);
                    intent.putExtra("uid", user.getUid());
                    startActivity(intent);
                }
			}
		});
    }

    @Override
    public void initData() {
        adapter.loadInitData();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        if (adapter != null) {
            adapter.doRefreshFooter();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        if (adapter != null) {
            adapter.loadInitData();
        }
    }
}
